﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XmlDataType;

namespace NewGame
{
    class Map : VisibleGameEntity3D
    {
        #region Attributes
        private MapTile[,] _mapTiles;
        private int _rows;
        private int _cols;
        private MapInfo _mapInfo;
        private Vector4 rectRegion;
        private Vector2 _size;
        #endregion

        #region Propertises
        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }
        #endregion

        #region Public Methods
        public Map(ContentManager content, string strHeighMap, float scale, Vector3 pos, float rotationY, string filePathMapInfo)
        {
            Texture2D textureHeightMap= content.Load<Texture2D>(strHeighMap);
            _rows = textureHeightMap.Height / Const.MAP_TILE_HEIGHT;
            _cols = textureHeightMap.Width / Const.MAP_TILE_WIDTH;
            _mapTiles = new MapTile[_rows, _cols];

            for (int i = 0; i < _rows; i++)
                for (int j = 0; j < _cols; j++)
                {
                    _mapTiles[i, j] = new MapTile(content, strHeighMap, i, j, scale, pos, rotationY, filePathMapInfo);
                    //_mapTiles[i, j].Position = new Vector3(i * scale * (Const.MAP_TILE_WIDTH -1) * Const.DISTANCE_OF_VERTEXT_IN_TERRAIN_MODEL, 0, j * scale * (Const.MAP_TILE_HEIGHT -1) * Const.DISTANCE_OF_VERTEXT_IN_TERRAIN_MODEL);
                }
            _mapInfo = content.Load<MapInfo>(filePathMapInfo);
            SetMapInfo(_mapInfo);
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < _rows; i++)
                for (int j = 0; j < _cols; j++)
                    _mapTiles[i, j].Update(gameTime);
        }

        public override void Draw(GameTime gameTime, GraphicsDevice graphicsDevice, Effect effect, MyCamera camera)
        {
            for (int i = 0; i < _rows; i++)
                for (int j = 0; j < _cols; j++)
                    _mapTiles[i, j].Draw(gameTime, graphicsDevice, effect, camera);
        }
        #endregion

        #region Private Methods
        public void SetMapInfo(MapInfo _mapInfo)
        {
            rectRegion = _mapInfo.ScaleMap * new Vector4(_mapInfo.X, _mapInfo.Y, _mapInfo.Width, _mapInfo.Height);
        }
        #endregion
    }
}
